import MainTableOperator from '../components/MainRowOperator';
import moment from 'moment';

export default class ProcessTmp {
    constructor(comp) {
        this.comp = comp;
    }

    // 给查询弹窗的申请日期添加默认值
    addDefaultSearchData = (template) => {
        template['search'].items.forEach((item) => {
            if (item.attrcode === 'apply_date' && !item.initialvalue) {
                let firstValue = moment().date(1).format('YYYY-MM-DD');
                let secondValue = moment().format('YYYY-MM-DD');

                item.initialvalue = {
                    display: `${firstValue},${secondValue}`,
                    value: `${firstValue},${secondValue}`
                };
            }
        });

        return template;
    }

    // 处理模版数据
    processTemplate = (template) => {
        const {props, action} = this.comp;
        const {
            emp,
            button
        } = props;

        // 给每个字段添加查询对象
        let formList = [];
        Object.keys(template).forEach((item) => {
            if (template[item] && template[item].moduletype === 'form') {
                formList.push(item);
            }
        });
        formList.forEach((item) => {
            template[item].items.forEach((item) => {
                if (!item.queryCondition) {
                    item.queryCondition = {};
                }
                if (item.attrcode === 'bill_code') {
                    item.disabled = true;
                }
            });
        });

        // 给主页面表格每行添加操作按钮
        template['list'].items.push({
            itemtype: 'customer',
            label: emp.language['gx6008-000046'],/* 国际化处理： 操作*/
            visible: true,
            fixed: 'right',
            width: '180px',
            attrcode: 'operation',
            render: (text, record, index) => {
                let billState = record.values['approve_state'].value;
                let btn = [];
                if (billState === '0') {
                    btn = ['row_approveinfo', 'row_copy', 'row_file_manager'];
                } else if (billState === '-1') {
                    btn = ['row_approveinfo', 'row_edit', 'row_delete', 'row_file_manager'];
                } else {
                    btn = ['row_approveinfo', 'row_file_manager'];
                }
                return (
                    <div className="currency-opr-col">
                        {button.createOprationButton(btn,
                            {
                                area: 'table',
                                buttonLimit: 1,
                                onButtonClick: function (props, id, e) {
                                    action.tableAct.rowClickHandle(id, record, index)
                                }
                            }
                        )}
                    </div>
                    /*<MainTableOperator
                        language={emp.language}
                        onClick={action.tableAct.rowClickHandle}
                        record={record}
                        index={index}
                        button={button}
                    />*/
                )
            }
        });

        // 给批量新增的查询弹窗某个字段添加查询条件
        template['search_batch'].items.forEach((item) => {
            if (item.attrcode === 'hi_psnjob.pk_org' || item.attrcode === 'hi_psnjob.pk_org.code') {
                if (!item.queryCondition) {
                    item.queryCondition = {};
                }
                Object.assign(item.queryCondition, {
                    TreeRefActionExt: 'nccloud.web.hrhi.pub.sqlbuilder.OrgRefSqlBuilder'
                });
            }
        })
        // 给查询弹窗某个字段添加查询条件
        template['search'].items.forEach((item) => {
            if (item.attrcode === 'pk_psnjob' || item.attrcode === 'pk_psnjob.clerkcode') {
                if (!item.queryCondition) {
                    item.queryCondition = {};
                }
                Object.assign(item.queryCondition, {
                    funcPK: '@@@@Z710000000006M0I'
                });
            }
        })


        template = this.addDefaultSearchData(template);

        return template
    }

}